package cn.maihe.elg.operation.service.order;

import cn.maihe.elg.operation.repository.entity.GuaranteeProgressInfoDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeProgressInfoDOCondition;
import cn.maihe.elg.operation.service.BaseDaoService;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 保函|受理订单 审核进度信息
 * @Author WJH
 * @Date 2021/05/08
 */
@Service
public class GuaranteeProgressInfoService extends BaseDaoService<GuaranteeProgressInfoDO> {


    public Integer getMaxStepByAcceptOrderNo(String acceptOrderNo) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("acceptOrderNo", acceptOrderNo);
        Integer maxStep = this.selectOneBySql("getMaxStepByAcceptOrderNo", params);
        return maxStep == null ? 0 : maxStep;
    }

    public void saveGuaranteeProgressInfo(GuaranteeProgressInfoDO curDo) {
        int nextStep = getMaxStepByAcceptOrderNo(curDo.getAcceptOrderNo())+1;
        curDo.setStep(nextStep);
        curDo.setGmtCreate(Date.from(Instant.now()));
        curDo.setGmtModified(curDo.getGmtCreate());
        this.insert(curDo);
    }

    public List<GuaranteeProgressInfoDO> selectGuaranteeProgressInfos(String acceptOrderNo) {
        return this.selectList(GuaranteeProgressInfoDOCondition.builder().andAcceptOrderNoEq(acceptOrderNo).build().orderDesc(GuaranteeProgressInfoDO::getStep));
    }

}
